import type { MockMethod } from "vite-plugin-mock";

let tagList = [
  {
    id: 1,
    name: "分类标签A",
    description: "用于商品分类的标签",
    type: 1, // 1:分类, 2:属性, 3:品牌, 4:促销
    color: "#3B82F6",
    weight: 8,
    status: 1, // 1:启用, 0:禁用
    sort: 10,
    productCount: 15,
    articleCount: 0, // 分类标签不关联文章
    create_time: "2023-01-01T10:00:00Z"
  },
  {
    id: 2,
    name: "属性标签B",
    description: "描述商品属性的标签",
    type: 2,
    color: "#FF5733",
    weight: 5,
    status: 1,
    sort: 20,
    productCount: 8,
    articleCount: 0, // 属性标签不关联文章
    create_time: "2023-01-05T11:30:00Z"
  },
  {
    id: 3,
    name: "品牌标签C",
    description: "关联特定品牌的标签",
    type: 3,
    color: "#4CAF50",
    weight: 7,
    status: 0,
    sort: 30,
    productCount: 22,
    articleCount: 0, // 品牌标签不关联文章
    create_time: "2023-01-10T14:00:00Z"
  },
  {
    id: 4,
    name: "促销标签D",
    description: "用于促销活动的标签",
    type: 4,
    color: "#9C27B0",
    weight: 9,
    status: 1,
    sort: 5,
    productCount: 30,
    articleCount: 12, // 促销标签可关联文章
    create_time: "2023-01-15T09:15:00Z"
  },
  {
    id: 5,
    name: "新品推荐",
    description: "推荐最新上架的商品",
    type: 1,
    color: "#00BCD4",
    weight: 10,
    status: 1,
    sort: 1,
    productCount: 12,
    articleCount: 0,
    create_time: "2023-02-01T08:00:00Z"
  },
  {
    id: 6,
    name: "热销爆款",
    description: "销量最高的商品",
    type: 4,
    color: "#FFC107",
    weight: 9,
    status: 1,
    sort: 2,
    productCount: 50,
    articleCount: 8, // 促销标签可关联文章
    create_time: "2023-02-05T16:45:00Z"
  },
  {
    id: 7,
    name: "环保材质",
    description: "采用环保材料制作的商品",
    type: 2,
    color: "#8BC34A",
    weight: 6,
    status: 1,
    sort: 25,
    productCount: 7,
    articleCount: 0,
    create_time: "2023-02-10T13:00:00Z"
  },
  {
    id: 8,
    name: "进口商品",
    description: "来自海外的优质商品",
    type: 3,
    color: "#607D8B",
    weight: 7,
    status: 1,
    sort: 35,
    productCount: 18,
    articleCount: 0,
    create_time: "2023-02-15T10:00:00Z"
  }
];

let nextTagId = tagList.length > 0 ? Math.max(...tagList.map(t => t.id)) + 1 : 1;

export default [
  // 获取标签列表
  {
    url: "/tag-management/tags",
    method: "get",
    response: ({ query }) => {
      const { name, type, status, page, pageSize } = query;
      let filteredList = tagList;

      if (name) {
        filteredList = filteredList.filter(tag => tag.name.includes(name));
      }
      if (type) {
        filteredList = filteredList.filter(tag => tag.type === parseInt(type));
      }
      if (status !== undefined && status !== null && status !== "") {
        filteredList = filteredList.filter(tag => tag.status === parseInt(status));
      }

      const total = filteredList.length;
      const startIndex = (parseInt(page) - 1) * parseInt(pageSize);
      const endIndex = startIndex + parseInt(pageSize);
      const list = filteredList.slice(startIndex, endIndex);

      return {
        code: 200,
        message: "success",
        data: {
          list: list,
          total: total
        }
      };
    }
  },
  // 添加标签
  {
    url: "/tag-management/tags",
    method: "post",
    response: ({ body }) => {
      const newTag = {
        id: nextTagId++,
        create_time: new Date().toISOString(),
        productCount: 0,
        ...body
      };
      tagList.push(newTag);
      return {
        code: 201,
        message: "添加成功",
        data: newTag
      };
    }
  },
  // 更新标签
  {
    url: "/tag-management/tags/:id",
    method: "put",
    response: ({ query, body }) => {
      const id = parseInt(query.id);
      const index = tagList.findIndex(tag => tag.id === id);
      if (index !== -1) {
        tagList[index] = { ...tagList[index], ...body };
        return {
          code: 200,
          message: "更新成功",
          data: tagList[index]
        };
      }
      return {
        code: 404,
        message: "标签未找到"
      };
    }
  },
  // 更新标签状态
  {
    url: "/tag-management/tags/:id/status",
    method: "put",
    response: ({ query, body }) => {
      const id = parseInt(query.id);
      const status = parseInt(body.status);
      const tag = tagList.find(t => t.id === id);
      if (tag) {
        tag.status = status;
        return {
          code: 200,
          message: "状态更新成功"
        };
      }
      return {
        code: 404,
        message: "标签未找到"
      };
    }
  },
  // 批量更新标签状态
  {
    url: "/tag-management/tags/batch-status",
    method: "put",
    response: ({ body }) => {
      const { ids, status } = body;
      ids.forEach((id: number) => {
        const tag = tagList.find(t => t.id === id);
        if (tag) {
          tag.status = status;
        }
      });
      return {
        code: 200,
        message: "批量状态更新成功"
      };
    }
  },
  // 删除标签
  {
    url: "/tag-management/tags",
    method: "delete",
    response: ({ body }) => {
      const ids = body.ids;
      const initialLength = tagList.length;
      tagList = tagList.filter(tag => !ids.includes(tag.id));
      if (tagList.length < initialLength) {
        return {
          code: 200,
          message: "删除成功"
        };
      }
      return {
        code: 404,
        message: "未找到要删除的标签"
      };
    }
  },
  // 根据标签ID获取商品列表 (模拟)
  {
    url: "/tag-management/goods-by-tag/:tagId",
    method: "get",
    response: ({ query }) => {
      const tagId = parseInt(query.tagId);
      // 模拟商品数据，实际应从商品列表中筛选
      const mockProducts = [
        {
          id: 101,
          sku: "SKU001",
          robotName: "智能扫地机器人",
          brand: "品牌A",
          category_name: "智能家电",
          price: 1999,
          createTime: "2023-01-01"
        },
        {
          id: 102,
          sku: "SKU002",
          robotName: "高清网络摄像头",
          brand: "品牌B",
          category_name: "安防设备",
          price: 599,
          createTime: "2023-01-05"
        },
        {
          id: 103,
          sku: "SKU003",
          robotName: "无线蓝牙耳机",
          brand: "品牌C",
          category_name: "数码配件",
          price: 299,
          createTime: "2023-01-10"
        },
        {
          id: 104,
          sku: "SKU004",
          robotName: "智能手环",
          brand: "品牌A",
          category_name: "穿戴设备",
          price: 399,
          createTime: "2023-01-15"
        },
        {
          id: 105,
          sku: "SKU005",
          robotName: "便携式充电宝",
          brand: "品牌D",
          category_name: "数码配件",
          price: 129,
          createTime: "2023-01-20"
        }
      ];
      const products = mockProducts.filter(p => p.id % tagId === 0); // 简单模拟关联
      return {
        code: 200,
        message: "success",
        data: {
          products: products,
          total: products.length
        }
      };
    }
  },
  // 添加商品到标签 (模拟)
  {
    url: "/tag-management/add-product-to-tag",
    method: "post",
    response: () => {
      return {
        code: 200,
        message: "商品关联成功"
      };
    }
  },
  // 从标签移除商品 (模拟)
  {
    url: "/tag-management/remove-product-from-tag",
    method: "post",
    response: () => {
      return {
        code: 200,
        message: "移除商品成功"
      };
    }
  },
  // 根据标签ID获取文章列表 (模拟)
  {
    url: "/tag-management/articles-by-tag/:tagId",
    method: "get",
    response: ({ query }) => {
      const tagId = parseInt(query.tagId);
      // 模拟文章数据，实际应从文章列表中筛选
      const mockArticles = [
        {
          id: 201,
          title: "春季促销活动攻略",
          category: "营销推广",
          author: "张三",
          status: "已发布",
          create_time: "2023-03-01"
        },
        {
          id: 202,
          title: "新品发布会预告",
          category: "产品资讯",
          author: "李四",
          status: "已发布",
          create_time: "2023-03-05"
        },
        {
          id: 203,
          title: "用户体验优化指南",
          category: "技术分享",
          author: "王五",
          status: "草稿",
          create_time: "2023-03-10"
        },
        {
          id: 204,
          title: "热销商品推荐榜单",
          category: "商品推荐",
          author: "赵六",
          status: "已发布",
          create_time: "2023-03-15"
        }
      ];
      // 只有促销标签(type=4)才关联文章
      const tag = tagList.find(t => t.id === tagId);
      const articles = tag && tag.type === 4 ? mockArticles.filter(a => a.id % tagId === 0) : [];
      return {
        code: 200,
        message: "success",
        data: {
          articles: articles,
          total: articles.length
        }
      };
    }
  },
  // 更新标签商品关联 (模拟)
  {
    url: "/tag-management/update-tag-product-association",
    method: "post",
    response: () => {
      return {
        code: 200,
        message: "更新商品关联成功"
      };
    }
  },
  // 更新标签文章关联 (模拟)
  {
    url: "/tag-management/update-tag-article-association",
    method: "post",
    response: () => {
      return {
        code: 200,
        message: "更新文章关联成功"
      };
    }
  },
  // 分配用户到标签 (模拟)
  {
    url: "/tag-management/assign-users-to-tag",
    method: "post",
    response: () => {
      return {
        code: 200,
        message: "用户分配成功"
      };
    }
  },
  // 获取用户标签列表 (模拟)
  {
    url: "/tag-management/user-tags",
    method: "get",
    response: () => {
      const mockUserTags = [
        { id: 1, name: "VIP用户", description: "高级会员标签" },
        { id: 2, name: "新注册用户", description: "新注册用户标签" }
      ];
      return {
        code: 200,
        message: "success",
        rows: mockUserTags,
        total: mockUserTags.length
      };
    }
  }
] as MockMethod[];
